# 24_utas2012.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-10

# package
require("ltm")
require("plink")
require("readr")
require("dplyr")
require("tidyr")
require("psych")


# Voters
## read dataset
voter2012 <- read_csv(
  "http://www.masaki.j.u-tokyo.ac.jp/utas/2012-2013UTASV131129.csv", 
  locale = locale(encoding = "CP932")
)

## specify variables
variables2012 <- c("Q013001", "Q013002", "Q013003", "Q013004", "Q013005",
                   "Q013006", "Q013007", "Q013008", "Q013009", "Q013010", 
                   "Q013011", "Q013012", "Q013013", "Q013014", "Q013015", 
                   "Q013016", "Q013017", "Q013018", "Q013019", "Q013020", 
                   "Q013021", "Q013022", "Q013023", "Q013024", "Q013025", 
                   "Q013026", "Q013101", "Q013102", "Q013103", "Q013104", 
                   "Q013105", "Q013106", "Q013107", "Q013108", "Q013109")

## handle missing data
for(i in variables2012) {
  n <- voter2012[,i] == 66 | voter2012[,i] == 99
  voter2012[n, i] <- NA
}
voter2012 <- voter2012[rowSums(voter2012[,variables2012], na.rm = TRUE) != 0,]

## estimate item parameters
irt2012_voter <- ltm::grm(voter2012[,variables2012], IRT.param = FALSE)
irt2012_voter ## viewing

## estimate voters ideology
score2012 <- ltm::factor.scores(irt2012_voter, voter2012[,variables2012])


# rescale
## common item
common2009 <- c("Q013801", "Q013802", "Q013804", "Q013805", "Q013806", 
                "Q013807", "Q013809", "Q013811", "Q013812", "Q013814", 
                "Q013816", "Q013817", "Q013818")
common2012 <- c("Q013001", "Q013002", "Q013003", "Q013004", "Q013005", 
                "Q013006", "Q013007", "Q013008", "Q013009", "Q013010", 
                "Q013016", "Q013017", "Q013015")
index <- data.frame(
  group1 = match(common2009, colnames(voter2009[,variables2009])), 
  group2 = match(common2012, colnames(voter2012[,variables2012]))
)
index

## list of parameters
### 2009
res2009 <- coef(irt2009_voter)
res2009 <- res2009[,c(5,1:4)]
colnames(res2009) <- c("a","b1","b2","b3","b4")
### 2012
res2012 <- coef(irt2012_voter)
res2012 <- res2012[,c(5,1:4)]
colnames(res2012) <- c("a","b1","b2","b3","b4")
### merge
pm <- list(res2009, res2012)

## list of scale
rescat <- list(rep(5, nrow(res2009)), 
               rep(5, nrow(res2012)))

## as.irt.pars
pm2009 <- as.poly.mod(n = nrow(res2009), model = "grm", 
                      items = 1:nrow(res2009))
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2009, pm2012), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2009$score.dat$z1, score2012$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2012_plink <- link.ability(plink_out)$group2
voter2012$Ideology <- -1 * score2012_plink

## Median
median(voter2012$Ideology)
by(voter2012$Ideology, voter2012$Q010200, describe)
